package com.jy.api.service.impl;

import com.jy.api.dao.ProductDao;
import com.jy.api.dao.StockCheckTransactionDao;
import com.jy.api.dao.StockTransactionDao;
import com.jy.api.domain.Product;
import com.jy.api.domain.StockChangeTransaction;
import com.jy.api.domain.StockCheckTransaction;
import com.jy.api.service.StockCheckService;
import com.jy.api.signUpReq.stock.StockCheckSignUpReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by lihu on 2018/2/2.
 */
@Service
public class StockCheckServiceImpl implements StockCheckService {

    @Autowired
    StockCheckTransactionDao stockCheckTransactionDao;

    @Autowired
    ProductDao productDao;

    @Autowired
    StockTransactionDao stockTransactionDao;


    @Override
    public void StockCheck(StockCheckSignUpReq signupReq) {
        /**
         * 所有库存变得记录
         */
        List<StockCheckTransaction> transactions = new ArrayList<>();
        /**
         * 商品出入库记录
         */
        List<StockChangeTransaction> stockTransactions = new ArrayList<>();
        /**
         * 库存变得商品
         */
        List<Product> products = new ArrayList<>();
        /**
         * 所有店铺商品
         */
        Map<String, Product> productMap = productDao.findListByStoreIdAndEnabledAndStockEnabled(
                signupReq.getStoreId(), true, true).
                stream().collect(Collectors.toMap(Product::getId, account -> account));
        for (StockCheckSignUpReq.CheckProduct checkProduct : signupReq.getProducts()) {
            /**
             * 修改商品数量
             */
            Product product = productMap.get(checkProduct.getProductId());
            product.setQuantity(checkProduct.getCheckTotalQty());
            products.add(product);

            /**
             * 如果数量改变则生成出入库记录
             */
            if (!checkProduct.checkSock()) {
                /**
                 * 生成出入库记录
                 */
                if (checkProduct.checkSockType()) {
                    StockChangeTransaction stockTransaction = new StockChangeTransaction(product, checkProduct, signupReq.getOperatorId(),
                            signupReq.getOperatorName(), signupReq.getStoreId(), product.getStockPrice(), signupReq.getId());
                    stockTransactions.add(stockTransaction);
                } else {
                    StockChangeTransaction stockTransaction = new StockChangeTransaction().StockIn(product, checkProduct, signupReq.getOperatorId(),
                            signupReq.getOperatorName(), signupReq.getStoreId(), product.getStockPrice(), signupReq.getId());
                    StockChangeTransaction stockTransaction1 = new StockChangeTransaction().StockOut(product, checkProduct, signupReq.getOperatorId(),
                            signupReq.getOperatorName(), signupReq.getStoreId(), product.getStockPrice(), signupReq.getId());
                    stockTransactions.add(stockTransaction);
                    stockTransactions.add(stockTransaction1);
                }
            }
            /**
             * 生成盘点记录
             */
            //StockCheckTransaction transaction = new StockCheckTransaction(signupReq.getId(), signupReq.getStoreId(), signupReq.getOperatorId(), signupReq.getOperatorName(), checkProduct);
            StockCheckTransaction transaction = new StockCheckTransaction();
            transactions.add(transaction);
        }
        productDao.save(products);
        stockCheckTransactionDao.save(transactions);
        stockTransactionDao.save(stockTransactions);

    }
}
